<template>
  <div class="dashboard-container">
    <h2 class="page-title">数据概览</h2>

    <!-- 统计卡片 -->
    <el-row :gutter="20" class="stats-row">
      <el-col :xs="24" :sm="12" :md="6">
        <div class="stat-card">
          <div class="stat-icon location-icon">
            <el-icon><Location /></el-icon>
          </div>
          <div class="stat-content">
            <p class="stat-label">地点总数</p>
            <h3 class="stat-value">{{ stats.totalLocations }}</h3>
          </div>
        </div>
      </el-col>

      <el-col :xs="24" :sm="12" :md="6">
        <div class="stat-card">
          <div class="stat-icon pending-icon">
            <el-icon><Clock /></el-icon>
          </div>
          <div class="stat-content">
            <p class="stat-label">待审核地点</p>
            <h3 class="stat-value">{{ stats.pendingLocations }}</h3>
          </div>
        </div>
      </el-col>

      <el-col :xs="24" :sm="12" :md="6">
        <div class="stat-card">
          <div class="stat-icon user-icon">
            <el-icon><UserFilled /></el-icon>
          </div>
          <div class="stat-content">
            <p class="stat-label">用户总数</p>
            <h3 class="stat-value">{{ stats.totalUsers }}</h3>
          </div>
        </div>
      </el-col>

      <el-col :xs="24" :sm="12" :md="6">
        <div class="stat-card">
          <div class="stat-icon rating-icon">
            <el-icon><Star /></el-icon>
          </div>
          <div class="stat-content">
            <p class="stat-label">评分总数</p>
            <h3 class="stat-value">{{ stats.totalRatings }}</h3>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 快捷操作 -->
    <div class="quick-actions">
      <h3 class="section-title">快捷操作</h3>
      <el-row :gutter="16">
        <el-col :xs="24" :sm="12" :md="6">
          <el-button type="primary" class="action-btn" @click="goTo('/locations/pending')">
            <el-icon><DocumentChecked /></el-icon>
            审核地点
          </el-button>
        </el-col>
        <el-col :xs="24" :sm="12" :md="6">
          <el-button type="success" class="action-btn" @click="goTo('/locations')">
            <el-icon><Location /></el-icon>
            管理地点
          </el-button>
        </el-col>
        <el-col :xs="24" :sm="12" :md="6">
          <el-button type="warning" class="action-btn" @click="goTo('/users')">
            <el-icon><User /></el-icon>
            用户管理
          </el-button>
        </el-col>
        <el-col :xs="24" :sm="12" :md="6">
          <el-button type="info" class="action-btn" @click="goTo('/ratings')">
            <el-icon><Star /></el-icon>
            评分管理
          </el-button>
        </el-col>
      </el-row>
    </div>

    <!-- 欢迎信息 -->
    <div class="welcome-card">
      <h3>欢迎使用清华美食地图管理后台</h3>
      <p>系统运行正常，当前时间：{{ currentTime }}</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  Location,
  Clock,
  UserFilled,
  Star,
  DocumentChecked,
  User
} from '@element-plus/icons-vue'
import { getStats } from '@/api/stats'

const router = useRouter()

const stats = ref({
  totalLocations: 0,
  pendingLocations: 0,
  totalUsers: 0,
  totalRatings: 0
})

const currentTime = ref('')
let timeInterval: ReturnType<typeof setInterval> | null = null

const goTo = (path: string) => {
  router.push(path)
}

const updateTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

/**
 * 加载统计数据
 */
const loadStats = async () => {
  try {
    const response = await getStats()
    if (response.success) {
      stats.value = {
        totalLocations: response.data.totalLocations,
        pendingLocations: response.data.pendingLocations,
        totalUsers: response.data.totalUsers,
        totalRatings: response.data.totalRatings
      }
    }
  } catch (error) {
    console.error('加载统计数据失败:', error)
    // 如果API不存在，使用模拟数据
    ElMessage.warning('统计数据加载失败，显示默认数据')
    stats.value = {
      totalLocations: 0,
      pendingLocations: 0,
      totalUsers: 0,
      totalRatings: 0
    }
  }
}

onMounted(() => {
  loadStats()
  updateTime()
  timeInterval = setInterval(updateTime, 1000)
})

onUnmounted(() => {
  if (timeInterval) {
    clearInterval(timeInterval)
  }
})
</script>

<style scoped>
.dashboard-container {
  max-width: 1400px;
  margin: 0 auto;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 24px;
}

.stats-row {
  margin-bottom: 24px;
}

.stat-card {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  display: flex;
  align-items: center;
  gap: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
  cursor: pointer;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: #fff;
}

.location-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.pending-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.user-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.rating-icon {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-content {
  flex: 1;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin: 0 0 8px;
}

.stat-value {
  font-size: 32px;
  font-weight: 700;
  color: #303133;
  margin: 0;
}

.quick-actions {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 20px;
}

.action-btn {
  width: 100%;
  height: 50px;
  font-size: 15px;
  border-radius: 8px;
}

.welcome-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  padding: 32px;
  color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.welcome-card h3 {
  font-size: 20px;
  margin: 0 0 12px;
}

.welcome-card p {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
}
</style>
